package cn.change.basic.controller;
import cn.change.basic.dto.RegisterDto;
import cn.change.basic.exception.BusinssException;
import cn.change.basic.service.VerifyCodeService;
import cn.change.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码集合
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     */
    @GetMapping("/image/{key}")
    public AjaxResult graph (@PathVariable("key") String key){
        try {
            String Base64Str = verifyCodeService.graph(key);
            return AjaxResult.me().setResultObj(Base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！！");
        }

    }
    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

}
